/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unit;

import javax.swing.JTextArea;
import jess.*;
import jess.swing.JTextAreaWriter;

/**
 *
 * @author soleman
 */
public class RuleChainer {

    private Rete Engine = new Rete();
    private Media media;
    private String fileruleName = new String();
    private Hobi hobi;
    private JTextAreaWriter jwriter;
    private JTextArea ouput = new JTextArea();

    public RuleChainer() {
        this.jwriter = new JTextAreaWriter(this.ouput);
        this.Engine.addOutputRouter("t", this.jwriter);
        this.Engine.addOutputRouter("WSTDOUT", this.jwriter);
        this.Engine.addOutputRouter("WSTDERR", this.jwriter);
    }

    public void SetRule(String filerulename) throws JessException {
        this.fileruleName = filerulename;
        this.Engine.batch(this.fileruleName);
    }

    public void run() throws JessException {
        this.Engine.run();
    }

    public void setInfoMedia(Media M) {
        this.media = M;
    }

    public void setHobi(Hobi h) {
        this.hobi = h;
    }

    public String ConvertFactMedia() {
        //Cek media
        String smedia = new String();
        String jurnalistik = new String();
        String info = new String();
        String profesional = new String();
        String broadcast = new String();
        String mediabroadcast = new String();
        String wartawan = new String();
        String bidang = new String();
        String tanyaTeknisBidang = new String();
        String tanyaTV = new String();
        String tanyaRadio = new String();
        String pengalamanPres = " (pengalaman 1)";//Di rule ada, tapi dari interviewer nggak ada.
        //Cek info media (valid)
        if (this.media.getSukaMedia() == 1) {
            smedia = " (media ya)";
        } else if (this.media.getSukaMedia() == 2) {
            smedia = " (media tidak)";
        }
        //Cek Jurnalistik (valid)
        if (this.media.getSukaJurnalistik() == 1) {
            jurnalistik = " (jurnalistik ya)";
        } else if (this.media.getSukaJurnalistik() == 2) {
            jurnalistik = " (jurnalistik tidak)";
        }
        //Cek info (valid)
        if (this.media.getInformasiMediaJurnalistik() == 1) {
            info = " (info itb)";
        } else if (this.media.getInformasiMediaJurnalistik() == 2) {
            info = " (info global)";
        }
        //Cek broadcast (valid)
        if (this.media.getSukaBroadcasting() == 1) {
            broadcast = " (broadcast ya)";
        } else if (this.media.getSukaBroadcasting() == 2) {
            broadcast = " (broadcast tidak)";
        }
        //Cek informasi wartawan (valid)
        if (this.media.getInformasiWartawan() == 1) {
            wartawan = " (wartawan 0)";
        } else if (this.media.getInformasiWartawan() == 2) {
            wartawan = " (wartawan 1)";
        }
        //Cek informasi Jenis media broadcast (valid)
        if (this.media.getJenisMediaBroadcast() == 1) {
            mediabroadcast = " (media-broadcast radio)";
        } else if (this.media.getJenisMediaBroadcast() == 2) {
            mediabroadcast = " (media-broadcast tv)";
        } else if (this.media.getJenisMediaBroadcast() == 3) {
            mediabroadcast = " (media-broadcast tidak)";
        }
        //Cek Penglaman Radio (valid)
        if (this.media.getPengalamanRadio() == 1) {
            profesional = " (profesional 0)";
        } else if (this.media.getPengalamanRadio() == 2) {
            profesional = " (profesional 1)";
        }
        //Cek Bidang Informasi (valid)
        if (this.media.getBidangInformasi() == 1) {
            bidang = " (pilih-bidang 0)";
        } else if (this.media.getBidangInformasi() == 2) {
            bidang = " (pilih-bidang 1)";
        } else if (this.media.getBidangInformasi() == 3) {
            bidang = " (pilih-bidang 2)";
        } else if (this.media.getBidangInformasi() == 4) {
            bidang = " (pilih-bidang 3)";
        }
        //Cek Jawaban Sejarah (valid)
        if (this.media.getJawabanSejarah() == 1) {
            tanyaTeknisBidang = " (tanya-global benar)";
        } else if (this.media.getJawabanSejarah() == 2) {
            tanyaTeknisBidang = " (tanya-global salah)";
        }
        //Cek Jawaban Politik (valid)
        if (this.media.getJawabanPolitik() == 1) {
            tanyaTeknisBidang = " (tanya-global benar)";
        } else if (this.media.getJawabanPolitik() == 2) {
            tanyaTeknisBidang = " (tanya-global salah)";
        }
        //Cek Jawaban Ekonomi (valid)
        if (this.media.getJawabanEkonomi() == 1) {
            tanyaTeknisBidang = " (tanya-global benar)";
        } else if (this.media.getJawabanEkonomi() == 2) {
            tanyaTeknisBidang = " (tanya-global salah)";
        }
        //Cek Jawaban Teknis Radio (valid)
        if (this.media.getTeknisRadio() == 1) {
            tanyaRadio = " (tanya-radio benar)";
        } else if (this.media.getTeknisRadio() == 2) {
            tanyaRadio = " (tanya-radio salah)";
        }
        //Cek Jawaban Teknis TV (valid)
        if (this.media.getTeknisTV() == 1) {
            tanyaTV = " (tanya-tv benar)";
        } else if (this.media.getTeknisTV() == 2) {
            tanyaTV = " (tanya-tv salah)";
        }
        return smedia + jurnalistik + info + wartawan + broadcast + mediabroadcast + profesional + bidang + tanyaTeknisBidang + tanyaTV + tanyaRadio + pengalamanPres;
    }

    public String ConvertFactHobi() {
        String hobiku = " (hobi ya)";
        String seni = new String();
        String jenisseni = new String();
        String jenismusik = new String();
        String mozart = new String();
        String jumlahpemain = new String();
        String speaking = new String();
        String bahasa = new String();
        String usaha = new String();
        String debate = new String();
        String bertahanhidup = new String();
        String dibentak = new String();
        String petualang = new String();
        String kamarMandi = new String();
        String Aliran = new String();
        String Angklung = new String();
        String Konduktor = new String();
        //Cek suka seni (valid)
        if (this.hobi.getSukaSeni() != 3) {
            seni = " (jenishobi seni)";
        }
        //Cek suka nyanyi atau main music (valid)
        if (this.hobi.getSukaSeni() == 1) {
            jenisseni = " (jenisseni 1)";
        } else if (this.hobi.getSukaSeni() == 2) {
            jenisseni = " (jenisseni 2)";
        }
        //Cek jenis music (valid)
        if (this.hobi.getJenisSeni() == 1) {
            jenismusik = " (jenismusik 2)";
        } else if (this.hobi.getJenisSeni() == 2) {
            jenismusik = " (jenismusik 1)";
        }
        //cek mozart (valid)
        if (this.hobi.getSukaMozart() == 1) {
            mozart = " (mozart 1)";
        }
        //Cek jumlah pemain (valid)
        if (this.hobi.getJumlahPemainMusik() == 1) {
            jumlahpemain = " (jumlah-pemain 4)";
        } else if (this.hobi.getJumlahPemainMusik() == 2) {
            jumlahpemain = " (jumlah-pemain 6)";
        }
        //Cek apakah suka speaking (valid)
        if (this.hobi.getSukaSpeaking() == 1) {
            speaking = " (jenishobi speaking)";
        }
        //Cek bahasa (valid)
        if (this.hobi.getSukaBahasa() == 1) {
            bahasa = " (bahasa 0)";
        } else if (this.hobi.getSukaBahasa() == 2) {
            bahasa = " (bahasa 1)";
        }
        //Cek wirausaha (valid)
        if (this.hobi.getSukaKewirausahaan() == 1) {
            usaha = " (usaha ya)";
        }
        //Cek debate  (valid)
        if (this.hobi.getSejarahDebat() == 1) {
            debate = " (debate ya)";
        }
        //Cek petualang (valid)
        if (this.hobi.getBertahanHidup() == 1) {
            bertahanhidup = " (bertahanhidup ya)";
        }
        //Cek dibentak (valid)
        if (this.hobi.getAksiDibentak() == 1) {
            dibentak = " (dibentak 0)";
        } else if (this.hobi.getAksiDibentak() == 2) {
            dibentak = " (dibentak 1)";
        }
        //Cek petualangan (valid)
        if (this.hobi.getSukaPetualang() == 1) {
            petualang = " (jenishobi petualang)";
        }
        //Cek Penyanyi Kamar Mandi (valid)
        if (this.hobi.getNyanyiKamarMandi() == 1) {
            kamarMandi = " (kamar-mandi 1)";
        } else if (this.hobi.getNyanyiKamarMandi() == 2) {
            kamarMandi = " (kamar-mandi 0)";
        }
        //Cek Aliran(valid)
        if (this.hobi.getAliran() == 1) {
            Aliran = " (aliran instrumental)";
        } else if (this.hobi.getAliran() == 2) {
            Aliran = " (aliran poprock)";
        }
        //Cek Angklung (valid)
        if (this.hobi.getSukaAngklung() == 1) {
            Angklung = " (angklung 1)";
        } else if (this.hobi.getSukaAngklung() == 2) {
            Angklung = " (angklung 0)";
        }
        //Cek Konduktor (valid)
        if (this.hobi.getMauConductor() == 1) {
            Konduktor = " (konduktor 1)";
        } else if (this.hobi.getMauConductor() == 2) {
            Konduktor = " (konduktor 0)";
        }
        return hobiku + seni + jenisseni + jenismusik + mozart + jumlahpemain + speaking + bahasa + usaha + debate + bertahanhidup + petualang + dibentak + kamarMandi + Aliran + Angklung + Konduktor;
    }

    public void addFact(String Facts) throws JessException {
        this.Engine.reset();
        String FactCollection = "(assert " + Facts + ")";
        this.Engine.eval(FactCollection);
        System.out.println(FactCollection);
        //this.Engine.eval("(facts)");
    }

    public String getOutput() {
        return this.ouput.getText();
    }
}
